﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;

namespace SilverlightCalculator
{
    [Export]
    public class CalculatorViewModel : EntityBase, INotifyPropertyChanged
    {
        private string _firstValue;
        private string _secondValue;
        private string _result;

        private readonly ICalculator _calculator;
        private readonly RelayCommand _calculateCommand;

        public event PropertyChangedEventHandler PropertyChanged;
        public ObservableCollection<string> FirstValueErrors { get; set; }
        private readonly ICalculatorValidator _calculatorValidator;

        [ImportingConstructor]
        public CalculatorViewModel(ICalculator calculator, ICalculatorValidator calculatorValidator)
        {
            _calculator = calculator;
            _calculatorValidator = calculatorValidator;
            _calculateCommand = new RelayCommand(Calculate) { IsEnabled = true };
        ErrorsChanged += new EventHandler<DataErrorsChangedEventArgs>(CalculatorViewModel_ErrorsChanged);

            _firstValue = "0";
            _secondValue = "0";
        }

        void CalculatorViewModel_ErrorsChanged(object sender, DataErrorsChangedEventArgs e)
        {
            _calculateCommand.IsEnabled = HasErrors == false;
        }

        public void Calculate()
        {
            Result = _calculator.Add(Convert.ToInt32(FirstValue), Convert.ToInt32(SecondValue)).ToString();
        }

        public string FirstValue
        {
            get { return _firstValue; }
            set
            {
                _firstValue = value;
                CheckIfNumberIsValid("FirstValue", value);
            }
        }

        public string SecondValue
        {
            get { return _secondValue; }
            set
            {
                _secondValue = value;
                CheckIfNumberIsValid("SecondValue", value);
            }
        }

        public void CheckIfNumberIsValid(string propertyName, string value)
        {
            ClearErrorFromProperty(propertyName);

            var validationResult = _calculatorValidator.Validate(value);
            if (validationResult.IsValid == false)
            {
                AddErrorForProperty(propertyName, validationResult.ErrorMessage);
            }
            OnPropertyChanged(propertyName);
        }

        public string Result
        {
            get { return _result; }
            private set
            {
                _result = value;
                OnPropertyChanged("Result");
            }
        }

        public RelayCommand CalculateCommand
        {
            get { return _calculateCommand; }
        }

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
